package com.dbqn.zhao.service.impl;

import com.dbqn.zhao.entity.User;
import com.dbqn.zhao.service.BaseService;
import com.dbqn.zhao.service.UserService;
import com.dbqn.zhao.util.ServiceFactory;

import java.util.List;
import java.util.Scanner;

public class LoginServiceIml implements BaseService, UserService {
    @Override
    public List execute(List list) {

        User user=new User();
        Scanner sc=new Scanner(System.in);

        for(;;){

            for (int i = 0; i <list.size() ; i++) {

                System.out.println(list.get(i));
            }

            System.out.println("请输入登录名：");
            String name=sc.next();
            System.out.println("请输入密码：");
            String password=sc.next();

            //判断密码长度和登录名是否为0
            if(name.trim().length()==0 && password.trim().length()==0){
                System.out.println("名称或者密码不能为空！");
                continue;
            }
            user.setName(name);
            user.setMima(password);


            User user1=login(list,user);

            if(user1==null){
                System.out.println("登录成功！");
                System.out.println("----------------------------");
                BaseService baseService = ServiceFactory.getService("xslx");
                baseService.execute(list);
                break;
            }else {
                System.out.println("不存在该用户！");
                break;
            }

        }

            return null;
    }

    @Override
    public int insert(List list) {
        return 0;
    }

    @Override
    public User login(List list,User user) {


        for (int i = 0; i <list.size() ; i++) {
            User user1=(User) list.get(i);
            if(user1.getName().equals(user.getName()) && user1.getMima().equals(user.getMima()) ){
                return null;
            }
        }
        return user;
    }
}
